import { defineMock } from '@alova/mock'
import { resultError, resultSuccess } from './_utils'

// 定义用户信息的类型
interface UserInfo {
  id: number
  username: string
  nickname: string
  avatar: string
}

// 定义用户对象的类型
interface Users {
  [key: string]: UserInfo
}

// 定义用户对象
const users: Users = {
  admin: {
    id: 1,
    username: 'admin',
    nickname: '超级管理员',
    avatar: '',
  },
}

export default defineMock({
  '[POST]/api/auth/login': ({ data }) => {
    const { username } = data
    const user = users[username]
    if (!user) {
      return resultError('用户不存在')
    }
    return resultSuccess({
      token: username,
    })
  },
  '[POST]/api/auth/logout': () => {
    return resultSuccess()
  },
  '/api/auth/info': ({ headers }) => {
    const { Authorization } = headers
    const user = users[Authorization]
    if (!user) {
      return resultError('用户不存在')
    }
    return resultSuccess(user)
  },
})
